﻿#ifndef YCBASEREQUESTTASK_H
#define YCBASEREQUESTTASK_H
#include "base/task/task.h"
#include "request.h"

namespace yc {

class RequestQueue;
class RequestTask : public Task
{
    Q_OBJECT

public:
    RequestTask(const Request& request, const ResponseHandler& handler, RequestQueue* queue,
                const RequestHeaders& global_headers, bool log, int timeout = 15000);
    ~RequestTask();

    const Request& request() const { return request_; }

Q_SIGNALS:
    void authenticationRequired();

private:
    void startImpl() override;
    void terminateImpl() override;
    void onTimeout() override;
    void onReplyError(QNetworkReply::NetworkError error);
    void onReplyFinished();
    void onResponse(const Request& request, const Response& response);
    void notifyUserCanceledError();
    void abortReply();

    RequestQueue* queue_;
    const RequestHeaders global_headers_;
    Request request_;
    ResponseHandler handler_;
    QNetworkReply* reply_;
    bool log_;
    bool completed_;
};

} // namespace yc

#endif // YCBASEREQUESTTASK_H
